<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 24/10/14
 * Time: 6:45 PM
 */




class Member_Register_Controller extends Base_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("default") ;
		$this->theme->set_layout("column1") ;
	}

	public function get_index(){

		$data['title'] = 'Member Login / Regsiter';

		/*-- META DATA */
		$this->theme->title($data['title'].' - '.Settings::get('en','site_title'));
		/*-- BREADCRUMB */
			
		Breadcrumb::addbreadcrumb('Home', URL::base());
		Breadcrumb::addbreadcrumb($data['title'], URL::base());
		Breadcrumb::setSeparator('&raquo;');
		$data['breadcrumb'] = Breadcrumb::generate();

		return $this->theme->render('member::register', $data);
	}

	public function post_index(){

		if(Input::get('btnLogin')){

			$rules = array(
					'username' => 'required|email',
					'password'		=> 'required',
			);
			// Validate all input
			$v = Validator::make(Input::all(), $rules);


			if( $v->fails() )
			{
					
			 return Redirect::back()
			 ->with_errors($v)
			 ->with_input();
			}
			else{
				$user = array(
						'username' => Input::get('username'),
						'password' => Input::get('password'),
				);

				if (Auth::attempt($user,true)) {
					if(Auth::user()->role_id == 1){
						return Redirect::to( '/admin' );
					}else{
						$uID = Auth::user()->id;

						$post_date = array(

								'updated_at' => Commoncall::current_timestamp(),
								'last_login_ip' => Commoncall::get_ip()
						);

						DB::table('users')->where('id','=',$uID)->update($post_date);

						return Redirect::to( '/member' );
					}
				}else{
						
					return Redirect::back()->with('msg','<div class="alert alert-danger">Invalid Input!</div>');
				}

			}
		}
			

		if(Input::get('btnRegister')){


			$rules = array(
					'fullname' => 'required',
					'email' => 'required|unique:users|email',
					'location' => 'required',
					'reg_password'         => 'required|min:6',
					'retype_password' => 'required|same:reg_password',
					'agree' => 'required',
			);

			$v = Validator::make(Input::all(), $rules);


			if( $v->fails() )
			{
					
			 return Redirect::back()
			 ->with_errors($v)
			 ->with_input();
			}
			else
			{

				Users::insert(array(

						'name' => Input::get('fullname'),
						//'username' => Input::get('fullname'),
						'email' => Input::get('email'),
						'password' => Hash::make(Input::get('reg_password')),
						'role_id' => '4',
						'location' => Input::get("location"),
						'agree' => Input::get('agree'),
						'status' => '51',
				));
					

					
				$emailTemp = DB::table('emailtemplate')->where('variable','=','registration_confirm')->first();


				$temTitle = $emailTemp->title;
				$temDesc = $emailTemp->description;

				$webTitle = Settings::get('en','site_title');

				$email = Input::get('email');

				$password = Input::get('password');

				
				
				$body = array(
						'website_title' => $webTitle,
						'member_name' => trim(Input::get('fullname')),
						'member_email' => trim(Input::get('email')),
						'member_password' => trim(Input::get('reg_password')),
						'website' => "<a href='".URL::base()."'>$webTitle</a>",
						'customer_care_no' => Settings::get('en','office_contact_1'),
				);


				$footer = Settings::get('en','mail_smtp_footer');


				$description = Parser::parse_string($temDesc, $body,TRUE);

				$description .= "<p>$footer</p>";
					
				$p = array(
						'from' => Settings::get('en','default_email'),
						'to' => trim(Input::get('email')),
						'sender_name' => trim($webTitle),
						'reply_to' => 'noreply@gmail.com',
						'subject' => trim($temTitle),
						'body' => trim($description),

				);

				Commoncall::mail($p);

					

			}

			$message = '<div class="alert alert-info">Thank you Please check your Email and Login.....!</div>';
			return Redirect::back()->with('message',$message);


		}

			
	}

	
}